﻿/*===========================================================================================
 * 
 *  Author          : Arturo Salvamante
 * 
 *  File            : apdu.cs
 * 
 *  Copyright (C)   : Advanced Card System Ltd
 * 
 *  Description     : Contains Methods and Properties for application protocol data unit
 * 
 *  Date            : June 03, 2011
 * 
 *  Revision Traile : [Author] / [Date if modification] / [Details of Modifications done]
 * 
 * 
 * =========================================================================================*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ACS
{
    public class Apdu
    {
        private byte _bCla;
        private byte _bIns;
        private byte _bP1;
        private byte _bP2;
        private byte _bP3;
        private int _LengthExpected;
        private int _actualLengthReceived;

        private byte[] _SendData;
        private byte[] _ReceiveData;
        private byte[] _Sw;


        /// <summary>
        /// The T=0 instruction class.
        /// </summary>
        public byte bCla
        {
            get { return _bCla; }
            set { _bCla = value; }
        }

        /// <summary>
        /// An instruction code in the T=0 instruction class.
        /// </summary>        
        public byte bIns
        {
            get { return _bIns; }
            set { _bIns = value; }
        }

        /// <summary>
        /// Reference codes that complete the instruction code.
        /// </summary>
        public byte bP1
        {
            get { return _bP1; }
            set { _bP1 = value; }
        }

        /// <summary>
        /// Reference codes that complete the instruction code.
        /// </summary>
        public byte bP2
        {
            get { return _bP2; }
            set { _bP2 = value; }
        }

        /// <summary>
        /// The number of data bytes to be transmitted during the command, per ISO 7816-4, Section 8.2.1.
        /// </summary>
        public byte bP3
        {
            get { return _bP3; }
            set { _bP3 = value; }
        }

        /// <summary>
        /// Length of data expected from the card
        /// </summary>
        public int lengthExpected
        {
            get { return _LengthExpected; }
            set { _LengthExpected = value; }
        }

        public int actualLengthReceived
        {
            get { return _actualLengthReceived; }
            set { _actualLengthReceived = value; }
        }

        public byte[] sendData
        {
            get { return _SendData; }
            set { _SendData = value; }
        }

        public byte[] receiveData
        {
            get { return _ReceiveData; }
            set { _ReceiveData = value; }
        }

        public byte[] sw
        {
            get { return _Sw; }
            set { _Sw = value; }
        }

        public void setCommand(byte[] cmd)
        {
            if (cmd.Length != 5)
                throw new Exception("Invalid command");

            bCla = cmd[0];
            bIns = cmd[1];
            bP1 = cmd[2];
            bP2 = cmd[3];
            bP3 = cmd[4];

            return;
        }

        public bool swEqualTo(byte[] data)
        {
            if (sw == null)
                return false;

            for (int i = 0; i < sw.Length; i++)
                if (sw[i] != data[i])
                    return false;

            return true;
        }
    }
}
